package seGobangClient.gaming;

import java.util.*;
import javax.vecmath.Point2i;
import seGobangClient.ISceneController;
import seGobangClient.Rule;

/**
 *
 * @author plus
 */
public class LocalPlayer implements IPlayer{
    private static ISceneController MyController;
    private String Name;
    private int iTurns, iRegrets;
    private int iTime;                                                          //剩余时间，单位s
    private byte chess_flag;
    private Timer timerUpdateInfo;
    private Rule MyRule;

    public LocalPlayer(String Name,byte chessflag,Rule rule) {
        this.chess_flag=chessflag;
        this.Name = Name;
        MyRule=rule;
        resetPlayer();
    }
    
    public void resetPlayer(){
        iTurns=0;
        iRegrets=MyRule.RegretTimes;
        iTime=MyRule.TimeLimit;
        if (timerUpdateInfo!=null) timerUpdateInfo.cancel();
    }
    
    public void setController(ISceneController Controller){
        MyController=Controller;
    }
    
    public int getLastRegrets() {
        return iRegrets;
    }

    public int getLastTime() {
        return iTime;
    }

    public String getPlayerName() {
        return Name;
    }

    public int getTurns() {
        return iTurns;
    }

    public void gameResult(int iResult) {
       if (timerUpdateInfo != null) timerUpdateInfo.cancel();
        System.err.println(this.getPlayerName()+" "+iResult);
    }
    
    public void turnToMe() {   
        timerUpdateInfo=new Timer();
        timerUpdateInfo.schedule(new TimerTask(){
            public void run(){
                MyController.doEvent("UpdatePlayerInfo", this);
                if (iTime==0){
                    MyController.doEvent("SomeoneTimeout", this);
                    timerUpdateInfo.cancel();
                }
                else 
                    if (iTime>0) iTime--;
            }
        }, 1000, 1000
        );
        MyController.doEvent("MarkCurrentPlayer", this);
        MyController.doEvent("EnableLocalInput",null);
    }

    public void turnOver(boolean isRegret) {
        if (timerUpdateInfo!=null) timerUpdateInfo.cancel();
       // timerUpdateInfo=null;
        
        if(isRegret)
           this.iTurns--; 
        else
            this.iTurns++;
        
    }

    public void putChess(Point2i pos) {
        if (this.chess_flag==-1)
            MyController.doEvent("BlackChessPut", pos);
        else
            MyController.doEvent("WhiteChessPut", pos);
    }

    public void regret() {
        if (iRegrets!=0)    {
            if(0!=MyController.doEvent("RaiseRegret", chess_flag)){
                iRegrets--;
            }
        }
    }

    public void surrender() {
        MyController.doEvent("RaiseSurrender", this);
    }
    
    public byte getChessFlag(){
        return this.chess_flag;
    }
    
}
